package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.pojos.NewsMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.NewsMaterialMapper;
import com.heima.wemedia.service.NewsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/24
 */
@Service
public class NewsMaterialServiceImpl extends ServiceImpl<NewsMaterialMapper,NewsMaterial> implements NewsMaterialService {

    @Autowired
    private NewsMaterialMapper newsMaterialMapper;

    @Override
    public boolean submit(Integer id, List<Integer> mIds) {
        LambdaQueryWrapper<NewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NewsMaterial::getNewsId,id);
        newsMaterialMapper.delete(queryWrapper);
        boolean flag = false;
        try {
            NewsMaterial newsMaterial = new NewsMaterial();
            Short type = 0;
            for (Integer mId : mIds) {
                newsMaterial.setMaterialId(mId);
                newsMaterial.setNewsId(Math.toIntExact(id));
                newsMaterial.setType(type);
                newsMaterial.setOrd(type);
                newsMaterialMapper.insert(newsMaterial);
                flag = true;
            }
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    @Override
    public void delete(Integer id) {
        LambdaQueryWrapper <NewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NewsMaterial::getNewsId,id);
        newsMaterialMapper.delete(queryWrapper);
    }
}
